function ScoreArea(userScore){
	this.file_cnt = 0;
	this.file_num = 0;
	this.userScore = userScore;
}

ScoreArea.borderStyle = ['solid 1px black', 'solid 3px red'];

/*******************************************
 * 处理用户拖入或写入TextArea中的数据
 *******************************************/
ScoreArea.prototype.handleFiles = function(files){
	this.file_num = files.length;
	this.file_cnt = 0;
    
	var file, filename;
	var that = this;
	
    for (var i = 0; i < this.file_num; i++) {
        file = files[i];
        filename = file.name;
        fileparts = filename.split('.');
        fileextname = fileparts[fileparts.length - 1];
		var reader = new FileReader();
        switch (fileextname) {
            case 'xls':
			case 'xlsx':
			case 'xlsb':
				reader.readAsBinaryString(file);
                reader.onload = function(e){
					that.pushData(this.result, 'xls');
                };
                break;
			case 'csv':
			case 'txt':
			default:
				//alert('文件格式错误，拖入的成绩单须为.xls或.txt格式！');
				reader.readAsText(file, 'gb2312');
                reader.onload = function(e){
					that.pushData(this.result, 'txt');
                }
                break;
        }
    }
}

ScoreArea.prototype.pushData = function(data, type){
	try{
		switch(type){
			case 'xls':
				this.userScore.pushXls(data);
				break;
			case 'txt':
				data = data.replace(/\r\n/g, '\n');
				this.userScore.pushTxt(data);
				break;
		}
		if(++this.file_cnt == this.file_num)
			this.userScore.send2Web();
	}catch(err){
		if(this.file_num == 1)
			alert(err);
	}
}

/*******************************************
 * TextArea处理事件：成绩输入管理
********************************************/
ScoreArea.handleDragover = function(e){
    e.stopPropagation();
    e.preventDefault();
    e.dataTransfer.dropEffect = 'copy';
    txtScoreArea.style.border = ScoreArea.borderStyle[1];
}

ScoreArea.handleDragleave = function(e){
    e.stopPropagation();
    e.preventDefault();
    e.dataTransfer.dropEffect = 'copy';
    txtScoreArea.style.border = ScoreArea.borderStyle[0];
}

ScoreArea.handleDrop = function(e){
    e.stopPropagation();
    e.preventDefault();
	new ScoreArea(new UserScore()).handleFiles(e.dataTransfer.files);
    txtScoreArea.style.border = ScoreArea.borderStyle[0];
}

ScoreArea.addEventListener = function(e){
	txtScoreArea.addEventListener('dragenter', ScoreArea.handleDragover, false);
	txtScoreArea.addEventListener('dragover', ScoreArea.handleDragover, false);
	txtScoreArea.addEventListener('drop', ScoreArea.handleDrop, false);
	txtScoreArea.addEventListener('dragleave', ScoreArea.handleDragleave, false);
}
